<template>
  <div class="xtx-goods-page" v-if="goods.categories">
    <div class="container">
      <!-- 面包屑 -->
      <!-- <XtxBread>
        <XtxBreadItem to="/">首页</XtxBreadItem>
        <XtxBreadItem to="/">手机</XtxBreadItem>
        <XtxBreadItem to="/">华为</XtxBreadItem>
        <XtxBreadItem to="/">p30</XtxBreadItem>
      </XtxBread> -->
      <!-- <pre>=={{ goods }}</pre> -->
      <XtxBread>
        <XtxBreadItem to="/">首页</XtxBreadItem>
        <XtxBreadItem :to="`/category/${goods.categories[1].id}`">
          {{ goods.categories[1].name }}
        </XtxBreadItem>
        <XtxBreadItem :to="`/category/sub/${goods.categories[0].id}`">
          {{ goods.categories[0].name }}
        </XtxBreadItem>
        <XtxBreadItem>
          {{ goods.name }}
        </XtxBreadItem>
      </XtxBread>
      <!-- 商品信息 -->
      <div class="goods-info">
        <!-- 图片预览区 -->
        <div class="media">
          <!-- 图片预览区 -->
          <!-- =={{ goods.mainPictures }} -->
          <!-- <GoodsImages :image="goods.mainPictures" /> -->
          <XtxImageView :imageList="goods.mainPictures"></XtxImageView>
          <!-- 统计数量 -->
          <GoodsSale :goods="goods"></GoodsSale>
        </div>
        <!-- 商品信息区 -->
        <div class="spec">
          <!-- 商品信息区 -->
          <GoodsInfo :goods="goods"></GoodsInfo>
          <!-- Sku组件 -->
          <GoodsSku
            :goods="goods"
            @change="changeSku"
            sku-id="1369155859933827074"
          ></GoodsSku>
          <XtxNumbox v-model="count" :max="countMax"></XtxNumbox>
          <!-- 按钮组件 -->
          <XtxButton
            @click="insertCart"
            type="primary"
            style="margin-top: 20px"
          >
            加入购物车
          </XtxButton>
        </div>
      </div>
      <!-- 商品详情 -->
      <div class="goods-footer">
        <div class="goods-article">
          <!-- 商品+评价 -->
          <!-- <div class="goods-tabs"></div> -->
          <!-- 商品详情 -->
          <GoodsDetail :goods="goods" />
        </div>
        <!-- 24热榜+专题推荐 -->
        <div class="goods-aside">
          <!-- 24热榜+专题推荐 -->
          <div class="goods-aside">
            <GoodsHot :type="1"></GoodsHot>
            <GoodsHot :type="2"></GoodsHot>
            <GoodsHot :type="3"></GoodsHot>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { reqFindGoods } from '@/api/goods'
import { ref, computed } from 'vue'
import { useRoute } from 'vue-router'
import GoodsInfo from './goods-info.vue'
import GoodsSale from './goods-sale.vue'
import GoodsDetail from './goods-detail.vue'
import GoodsHot from './goods-hot.vue'
import GoodsSku from './goods-sku.vue'
import { message } from '@/components/Message'
import { useStore } from 'vuex'
export default {
  name: 'XtxGoodsPage',
  components: {
    GoodsInfo,
    GoodsSale,
    GoodsSku,
    GoodsDetail,
    GoodsHot
  },
  setup () {
    const goods = ref({})
    const count = ref(1)
    const route = useRoute()
    async function getGoods () {
      const res = await reqFindGoods(route.params.id)
      goods.value = res.result
    }
    const currSku = ref({})
    const changeSku = (sku) => {
      if (sku.skuId) {
        goods.value.price = sku.price
        goods.value.oldPrice = sku.oldPrice
        goods.value.inventory = sku.inventory
      }
      currSku.value = sku
    }
    const countMax = computed(() => {
      return goods.value.inventory > 0 ? goods.value.inventory : 1
    })
    getGoods()
    const store = useStore()

    const insertCart = async () => {
      // console.log(!currSku.value.skuId, 7777)
      if (!currSku.value.skuId) {
        return message({ type: 'error', text: '请选择规格' })
      }
      // 商品库存(加入购物车的数量不能大于库存)
      if (count.value > goods.value.inventory) {
        return
      }
      await store.dispatch('cart/asyncInsertCart', {
        id: goods.value.id,
        name: goods.value.name,
        picture: goods.value.mainPictures[0],
        skuId: currSku.value.skuId,
        price: currSku.value.oldPrice,
        nowPrice: currSku.value.price,
        attrsText: currSku.value.specsText,
        stock: currSku.value.inventory,
        selected: true, // 选中
        isEffective: true, // 是否有效
        count: count.value // 商品数量
      })
      message({ type: 'success', text: '加入购物车成功' })
      // currSku.value = {}
    }
    return {
      goods,
      changeSku,
      countMax,
      count,
      insertCart
    }
  }
}
</script>

<style scoped lang="less">
.goods-info {
  min-height: 600px;
  background: #fff;
  display: flex;
  .media {
    width: 580px;
    height: 600px;
    padding: 30px 50px;
  }
  .spec {
    flex: 1;
    padding: 30px 30px 30px 0;
  }
}
.goods-footer {
  display: flex;
  margin-top: 20px;
  .goods-article {
    width: 940px;
    margin-right: 20px;
  }
  .goods-aside {
    width: 280px;
    min-height: 1000px;
  }
}
.goods-tabs {
  min-height: 600px;
  background: #fff;
}
.goods-warn {
  min-height: 600px;
  background: #fff;
  margin-top: 20px;
}
</style>
